package com.wink.service;

import com.google.gson.Gson;
import com.storage.memory.FakeData;
import com.storage.memory.StorageMemory;
import com.wink.charity.Charity;
import com.wink.charity.CharityList;
import com.wink.dt.Comment;
import com.wink.dt.Topic;
import com.wink.dt.TopicList;
import com.wink.event.Event;
import com.wink.event.EventList;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.ws.rs.FormParam;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriInfo;

import org.apache.wink.common.model.synd.SyndContent;
import org.apache.wink.common.model.synd.SyndEntry;
import org.apache.wink.common.model.synd.SyndText;
import org.apache.wink.server.utils.LinkBuilders;

public class CommunityServicesImpl implements CommunityServices {

	// POS singleton (better solution should be
	// provided!)
	private static CommunityServicesImpl svc;

	// simulate persistent storage (singleton svc ensures only one copy)
	private StorageMemory storage = new StorageMemory();

	public synchronized static CommunityServicesImpl getInstance() {
		if (svc == null) {
			System.out.println("---> SVR: creating demo data");

			// TODO here we should add the storage the service 
			svc = new CommunityServicesImpl();

			// we need data for demonstration	
			List<Charity> cs = FakeData.generateCharities();
			for (Charity c : cs)
				svc.storage.add(c);
			
			List<Event> es = FakeData.generateEvents();
			for (Event e : es)
				svc.storage.add(e);
			
			List<Topic> ts = FakeData.generateTopics();
			for (Topic t : ts)
				svc.storage.add(t);
		}

		return svc;
	}	
	
	@Override
	public String getServices() {	
		System.out.println("---> SVR: In getServices ");
		String[] services =  {"DiscussionThreads", "Events"};
		ToConverter obj = new ToConverter(services);		
		Gson gson = new Gson();
		String json = gson.toJson(obj);		
		return json;
	}
		
	//Charity actions
	@Override
	public String listAllCharity() {
		System.out.println("---> SVR: In listAllCharity ");
		ArrayList<Charity> list = storage.getCharities();
		//System.out.println("---> SVR: In listCharities list size " + list.size()); 
		
		CharityList cList = new CharityList(list);
		//hook to storage
		return CharityList.toJson(cList);
	}			
	
	@Override
	public String createCharity(String json)  {
		Charity charity = Charity.fromJson(json);
		System.out.println("---> SVR: In createCharity");
		System.out.println("---> SVR: createCharity --> "
				+ ", Charity Name" + charity.getName() 
				+ ", Charity Description" + charity.getDescription());
		//hook to storage
		return Charity.toJson(charity);
	}
	
	@Override
	public String GetCharityByName(@PathParam("name") String name ) {
		System.out.println("---> SVR: In GetCharityByName");
		System.out.println("---> SVR: In parm " + name);
		String cs = listAllCharity();
		return cs;
		/*CharityList cList = CharityList.fromJson(cs);
		ArrayList<Charity> list = cList.getList();
		for (Charity c : list)
		{
			if (c.getName().equalsIgnoreCase(name.trim())) {
				ArrayList<Charity> list2 = new ArrayList<Charity>();
				list2.add(c);
				CharityList cl = new CharityList();
				cl.setData(list2);
				return cl;
			}				
		}		
		return null;*/			
	}
		
	@Override
	public String donate(String charity) {		
		System.out.println("---> SVR: In donate"  );
		Charity c = Charity.fromJson(charity);
		System.out.println("---> SVR: Donate to  --> "
				+ ", Charity Name " + c.getName() 
				+ ", amt " + c.getAmount());
		return Charity.toJson(c);
		//hook to storage
	}	
	
	//DT actions
	@Override
	public String listAllDiscussion() {
		System.out.println("---> SVR: In listAllDiscussion");
		ArrayList<Topic> list = storage.getTopics();
		
		TopicList tList = new TopicList(list);	

		return TopicList.toJson(tList);
	}
	
	@Override
	public String createTopic(String json) {				
		Topic topic = Topic.fromJson(json);
		System.out.println("---> SVR: In createTopic");
		System.out.println("---> SVR: createTopic --> "
				+ ", Charity Name" + topic.getTopicTitle() 
				+ ", Charity Description" + topic.getTopicdescription());
		//hook to storage
		return Topic.toJson(topic);
	}
	
	@Override
	public void createComment( String json) {	
		Comment comment = Comment.fromJson(json);
		System.out.println("---> SVR: In createComment");
		System.out.println("---> SVR: createComment --> "
				+ ", CommentTopic Name" + comment.getTopicName() 
				+ ", Comment Description" + comment.getComment());
		//hook to storage
		//return Comment.toJson(topic);	
	}		

	@Override
	public String getTopicByName(@PathParam("name") String name ) {
		System.out.println("---> SVR: In getTopicByName, " + name);				
		String tlist = listAllDiscussion();
		TopicList topicList = TopicList.fromJson(tlist);
		ArrayList<Topic> list = topicList.getList();
		for (Topic c : list)
		{
			if (c.getTopicdescription().equalsIgnoreCase(name.trim())) {
				ArrayList<Topic> list2 = new ArrayList<Topic>();
				list2.add(c);
				TopicList cl = new TopicList();
				cl.setData(list2);
				return TopicList.toJson(cl);
			}				
		}		
		return null;			
	}		
	
	@Override
	public String getTopicDetails(@PathParam("name") String name ) {
		System.out.println("---> SVR: In getTopicDetails, " + name);	
		String tlist = listAllDiscussion();
		TopicList topicList = TopicList.fromJson(tlist);
		ArrayList<Topic> list = topicList.getList();
		for (Topic c : list)
		{
			if (c.getTopicdescription().equalsIgnoreCase(name.trim())) {				
				return Topic.toJson(c);
			}				
		}	
		return null;
	}
	
	public static void main(String[] args) {
		//new CommunityServicesImpl().getServices();
	}
	
	
}

class ToConverter
{
	public String service1 ,service2;
	
   public ToConverter(String[] convert)
   {
	   service1 = convert [0];
	   service2 = convert [1];	   
   }
}
